﻿#region 命名空间

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using WinnerCMS.Model;

#endregion

namespace WinnerCMS.BLL.Config
{
    public class UI
    {
        /// <summary>
        /// 获取分类
        /// </summary>
        /// <returns></returns>
        public List<KeyValue<string, string>> GetTabs()
        {
            List<KeyValue<string, string>> dic = new List<KeyValue<string, string>>
            {
                new KeyValue<string, string>("配置", typeof (CMSConfig).FullName)
            };

            var propertys = typeof (CMSConfig).GetProperties();

            foreach (var p in propertys)
            {
                var category = p.GetCustomAttributes<CategoryAttribute>(true).FirstOrDefault();
                var displayName =
                    p.GetCustomAttributes<DisplayNameAttribute>(true).FirstOrDefault();
                if (category == null) continue;
                dic.Add(new KeyValue<string, string>(displayName.DisplayName, p.Name));
            }
            return dic;
        }

        public PropertyInfo[] GetValueTypeProperties(string propertyname)
        {
            var config = CMSConfig.Instance();
            Dictionary<string, object> dic = new Dictionary<string, object>();


            var property = config.GetType().GetProperty(propertyname);

            object shili;

            if (property == null)
            {
                shili = config;
            }
            else
            {
                shili = property.GetValue(config, null);
            }

            return shili.GetType().GetProperties();
        }


        public void GetData(NameValueCollection form)
        {
        }

        public static object GetInstance(string propertyname)
        {
            var config = CMSConfig.Instance();
            var property = config.GetType().GetProperty(propertyname);

            object shili;

            if (property == null)
            {
                shili = config;
            }
            else
            {
                shili = property.GetValue(config, null);
            }
            return shili;
        }
    }
}